local SQL           = require "sql"
local show          = require "showmsg" 

-- 全局 sql 设置
global_db_hostname    = "127.0.0.1"
global_db_port        = 3306
global_db_username    = "root"
global_db_password    = "123123"
global_db_database    = "loginlog"
    
-- 局部 sql 设置
local log_db_hostname   = ''
local log_db_port       = 3306
local log_db_username   = ''
local log_db_password   = ''
local log_db_database   = ''

local loginlog_table = "loginlog"
local sql 
local enabled = false

local loginlog = {}

-- 返回IP在最后几分钟内失败的登录尝试次数
function loginlog:failedattempts(ip, minutes)

   local  failures = 0

   if not enabled then

        return false
   end 

   if SQL_ERROR == SQL:query(sql, "SELECT count(*) FROM `%s` WHERE `ip` = '%s' AND `rcode` = '1' AND \
                                `time` > NOW() - INTERVAL %d MINUTE", loginlog_table, ip, minutes) then 

        SQL:show_debug(sql, "loginlog.lua -> loginlog_failedattempts", "查询失败")
   end 

   if SQL:num_rows(sql) > 0  then 
        
        SQL:get_data(sql)
        failures = tonumber(sql.data[1])
        SQL:free_result(sql)
   end 
   
   return failures
end


--登录日志中记录事件
function loginlog:log(ip, name, rcode, message)

        -- insert  --> 

        if not enabled then 
           return 
        end 

        if SQL_ERROR == SQL:query(sql, "INSERT INTO `%s`(`time`, `ip`, `user`, `rcode`, `log`) \
                                        VALUE (NOW(), '%s', '%s','%d', '%s')", 
                                        loginlog_table, ip, name, rcode, message ) then 
        
            SQL:show_debug(sql, "loginlog.lua", "插入语句错误")
        end 
end 

-- 初始化
function loginlog:init()

            
    local conf  = {}

    if log_db_hostname ~= '' then 

        -- 局部
        conf.host     = log_db_hostname
        conf.port     = log_db_port
        conf.basename = log_db_database
        conf.user     = log_db_username
        conf.pawd     = log_db_password 

    else 
        -- 全局
        conf.host     = global_db_hostname
        conf.port     = global_db_port
        conf.basename = global_db_database
        conf.user     = global_db_username
        conf.pawd     = global_db_password
    end 

    sql = SQL:new_sql_handler()

    if SQL_ERROR == SQL:connect_db(sql, conf) then 

        SQL:show_debug(sql, "account_sql.lua -> loginlog_init ", "连接数据库失败")
        return false
    end

    show:info("连接到主数据库 %s", conf.basename) 

    enabled = true 

    return true  
end 

-- 关闭相关的操作
function loginlog:final()

   SQL:close_db(sql)
   sql = nil
   return true 
end 

--读取登录或日志配置
function loginlog:config_read(key, value)

    local signature
    local prefix,left 

    signature = "sql."    
    prefix = string.sub(key, 1, string.len(signature))
    left   = string.sub(key, string.len(signature)+1) 

    if signature == prefix then 

        if  left == "db_hostname"   then  global_db_hostname = value                    else  end 
        if  left == "db_port"       then  global_db_port     = tonumber(value) or 3306  else  end 
        if  left == "db_username"   then  global_db_username = value                    else  end 
        if  left == "db_password"   then  global_db_password = value                    else  end 
        if  left == "db_database"   then  global_db_database = value                    else  return false  end  -- false not found
        return true 
    end 

    if key == "log_db_ip"     then  log_db_hostname = value                   else end 
    if key == "log_db_port"   then  log_db_port     = tonumber(value) or 3306 else end 
    if key == "log_db_id"     then  log_db_username = value                   else end 
    if key == "log_db_pw"     then  log_db_password = value                   else end 
    if key == "db_database"   then  log_db_database = value                   else return false end 

    return true 
end 

return M